// 任务
// noinspection JSUnusedLocalSymbols

import {ElementUtils} from "@/assets/js/ElementUtils";
import {MissionEvent} from "@ginstone/nga-api/dist/src/enums/MissionEvent";
import {MissionType} from "@ginstone/nga-api/dist/src/enums/MissionType";
import {NukeApi} from "@ginstone/nga-api/dist/src/method/NukeApi";
import {MissionCheckInParam, MissionListParam} from "@ginstone/nga-api/dist/src/params/NukeParams";

export default {
    namespaced: true,
    state: {},
    mutations: {
        method(state, payload) {

        },
    },
    actions: {
        method: ({dispatch, commit, state, rootGetters}, payload) => {

        },
        // 自动完成所有客户端的签到任务
        missionAutoCheckInAll: ({dispatch, commit, state, rootGetters}) => {
            const clients = rootGetters["Account/getAllClients"];
            clients.forEach(client => dispatch("missionAutoCheckIn", client))
        },
        // 查询、并自动完成任务
        missionAutoCheckIn: ({dispatch, commit, state, rootGetters}, client) => {
            client = client || rootGetters["Account/getClient"]();
            dispatch("missionList", client).then(async res => {
                // 未完成任务
                const missions = res.filter(i => !i.missionStatus.countedToday)
                //自动完成任务
                for (const key in missions) {
                    const mission = missions[key]
                    const {name, id: mid} = mission
                    const r = await dispatch("missionCheckIn", {client, mid})
                    ElementUtils.notice({
                        title: `${client.username}: ${name}`,
                        message: r.message,
                    })
                }
            })
        },
        // 完成并提交签到任务
        missionCheckIn: ({dispatch, commit, state, rootGetters}, {client, mid}) => {
            return NukeApi.missionCheckIn(client || rootGetters["Account/getClient"](), new MissionCheckInParam(mid))
        },
        // 查询签到任务列表
        missionList: ({dispatch, commit, state, rootGetters}, client) => {
            return NukeApi.missionList(client || rootGetters["Account/getClient"](), new MissionListParam(MissionEvent.SIGN_IN, MissionType.COUNTER))
        },
    },
    getters: {},
}